package morrislabs.controls
{
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import mx.containers.HBox;
	import mx.events.FlexEvent;

	public class TimeInput extends HBox
	{
		private const HH:int = 0;  // hours
		private const MM:int = 1;  // minutes
		private const SS:int = 2;  // seconds
		private const FF:int = 3;  // fraction of second
		
		private var boxBackground: uint;
		private var boxBorder: uint;
		private var numBackground: uint;
		private var numColor: uint;
		private var numAlpha:Number;
		private var _allSelected:Boolean = false;
		private var _value:Number;
		private var _text:String;
				
		public var timeElements: Array = [{handle: NumericInput, id:"hours", min:0, max:23, value:0},
										  {handle: NumericInput, id:"minutes", min:0, max: 59, value:0},
										  {handle: NumericInput, id:"seconds", min:0, max:59, value:0},
										  {handle: NumericInput, id:"fraction", min:0, max:999, value:0}];
		public var labelSeparators:Array = new Array();

		
		public function TimeInput()
		{
			super();
			
			// set hbox properties
			width = 145;
			height = 20;
			doubleClickEnabled = true;
			setStyle("backgroundColor","#FFFFFF");
			setStyle("horizontalGap",0);
			setStyle("borderStyle","inset");
			setStyle("borderThickness",0);
			setStyle("horizontalAlign","center");
			setStyle("verticalAlign","middle");
			setStyle("paddingLeft",0);
			setStyle("paddingRight",0);
			setStyle("fontSize",11);
			
			addEventListener(FlexEvent.CREATION_COMPLETE, creationHandler);
			addEventListener(FocusEvent.FOCUS_IN, focusInEvents);	
			addEventListener(FocusEvent.FOCUS_OUT, focusOutEvents);			
			addEventListener(KeyboardEvent.KEY_DOWN, keyDownEvents);
			addEventListener(MouseEvent.DOUBLE_CLICK, mouseDoubleClickEvent);
			addEventListener(MouseEvent.CLICK, mouseClickEvent);
		}
		
 		// Gets called after the component has been initialized
        private function creationHandler(event:FlexEvent):void
        {
        	var fontH:int = (getStyle("fontSize") as int);
        	
         	for (var i:int = 0; i < timeElements.length; i++)
        	{
   				timeElements[i].handle = (this.getChildByName(timeElements[i].id) as NumericInput);
   				timeElements[i].handle.maxValue = timeElements[i].max;
   				timeElements[i].handle.value = timeElements[i].value;
   				timeElements[i].handle.setStyle("fontSize", fontH);
     			timeElements[i].handle.toolTip = 
     			(timeElements[i].min + "-" + timeElements[i].max);   				
        	}

  			boxBackground = getStyle("backgroundColor") as uint;
  			boxBorder = getStyle("borderColor") as uint;
  			
  			with (timeElements[0].handle as NumericInput) 
  			{
        		numBackground = getStyle("backgroundColor");
        		numColor = getStyle("color");
        		numAlpha = alpha;
     		}
     		
     		timeElements[timeElements.length-1].handle.isLastInput = true;
        }
        
       	private function focusInEvents( event:FocusEvent ) : void 
       	{        
       		setStyle("borderThickness",5);
        	setStyle("borderColor","#1298EE");
        	timeElements[timeElements.length-1].handle.isLastInput = true;
        }
        
        private function focusOutEvents( event:FocusEvent ) : void 
        {
        	setStyle("borderThickness",0);
        	setStyle("borderColor", boxBorder);
        }
        
        
       	private function keyDownEvents( event:KeyboardEvent ) : void 
       	{
        	switch(event.keyCode) {
       			case Keyboard.HOME:
 					focusManager.setFocus(timeElements[0].handle);
					break;
        		case Keyboard.END:
 					focusManager.setFocus(timeElements[timeElements.length-1].handle);
					break;      				
    			
    			case Keyboard.ENTER:
    				timeElements[timeElements.length-1].handle.isLastInput = false;
    				focusManager.setFocus(timeElements[timeElements.length-1].handle);
   					focusManager.moveFocus(mx.events.FocusRequestDirection.FORWARD);
    				break;	      				
       		}
       	} 
       	
       	private function mouseDoubleClickEvent( event:MouseEvent ) : void {
       		this.selectAll(!_allSelected);
       	}
       	  
       	private function mouseClickEvent( event:MouseEvent ) : void {
       		this.selectAll(false);
       	}      
       	       	
       	public function selectAll(select:Boolean) : void 
       	{
   			for (var i:int = 0; i < timeElements.length; i++)
    		{
    			with (timeElements[i].handle as NumericInput) {
    				if (select) {
   						setStyle("backgroundColor","#000000");
   						alpha = 1.0;
   						setStyle("color","#FFFFFF");
    				} else {
   						setStyle("backgroundColor",numBackground);
   						alpha = numAlpha;
   						setStyle("color",numColor);        					
    				}
    			}
    		}
 
			_allSelected = select;
       	}  
       	
       	
       	public function get text():String 
       	{
       		_text = (timeElements[0].handle as NumericInput).text + ":" +
       				(timeElements[1].handle as NumericInput).text + ":" +
       				(timeElements[2].handle as NumericInput).text + "." +
       				(timeElements[3].handle as NumericInput).text;
        	return _text;
        }
		
		
		[Bindable("change")]
	    [Inspectable(category="General", defaultValue="true")]		
        public function set text(newValue:String):void 
        { 		
        	timeElements[0].handle.value = parseInt(newValue.substr(0,2));
        	timeElements[1].handle.value = parseInt(newValue.substr(3,2));
        	timeElements[2].handle.value = parseInt(newValue.substr(6,2));
        	timeElements[3].handle.value = parseInt(newValue.substr(9,3));
        		
			_text = newValue;
			
        }
        
        

	}
}